package com.hjt.gulimall.cart.vo;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车
 * @author:97007
 * @date:2021/10/1 9:32
 */
@Data
public class Cart {
    List<CartItem> items;

    private Integer countNum; // 商品数量
    private Integer countType; // 商品类型数量
    private BigDecimal totalAmount;//  商品总价
    private BigDecimal reduce = new BigDecimal(0); // 减免价格

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int totalNum = 0;
        for (CartItem item : items) {
            totalNum += items.size();
        }
        return totalNum;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        return items!=null?items.size():0;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        // 计算总价格
        BigDecimal totalAmount = new BigDecimal(0);
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                totalAmount = totalAmount.add(item.getTotalPrice());
            }

        }
        // 减去优惠价格
        return totalAmount.subtract(reduce);
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
